package UILayer;
import java.util.Scanner;
import ControlLayer.*;
/**
 * This class contains the systems tui. Besides of that, it sends a number of method-calls to the Order-Controller.
 */
public class OrderUI
{
    private OrderCtr oCtr; //instance variable
    public OrderUI()
    {
        //controller
    }

    public void start()
    {
        oCtr = new OrderCtr(); //creates new objekt of type OrderCotroller
        orderMenu();    //starts the method orderMenu()
    }

    public void orderMenu()
    {
        boolean exit = false;
        while(!exit) //while exit not is true, runs the code beneth
        {
            int choice = showMenu(); //show the menu, from the showMenu() method

            if (choice == 1)  //let's you make a choice. choice is made in the showMenu() methode
            {
                findProduct();
            }
            else{
                if(choice == 2){
                    addProduct();
                }
                else{
                    if(choice == 3){
                        addCustomer();
                    }
                    else{
                        if(choice==4){
                            newCustomer();
                        }
                        else{
                            if(choice==5){
                                showOrder();
                            }
                            else{
                                if(choice==6){
                                    checkOut();
                                }
                            else{
                                exit = true; //everything else than 1-6 will make the system exit
                            }//end else
                        }//end else
                    }//end else
                }//end while
            }//end method
        }
    }
}

    private int showMenu()  //prints the menu to screen. Recieves the next number(s) you press, and returns it to orderMenu() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ***Order Menu ***");
        System.out.println(" (1) Find products");
        System.out.println(" (2) Add product");
        System.out.println(" (3) Log in OR (4) create new account");
        System.out.println(" (5) Show order info");
        System.out.println(" (6) Check out");
        System.out.println(" (7) Exit");

        int choice = keyboard.nextInt();
        return choice;
    }

    private void findProduct() //method-call to order-controller, to find a product
    {
        System.out.println("Search for product: [enter search criteria]");
        String productName = inputString();
        oCtr.findProduct(productName);
        inputDefault();
    }

    private void addProduct()  //method-call to order-controller, to add a product to the order
    {
        System.out.println("Add product to order: [enter the products id");
        int productId = inputInt();
        System.out.println("& [enter the amount]");
        int amount = inputInt();
        oCtr.getProduct(productId, amount);
        inputDefault();
    }

    private void addCustomer()  //method-call to order-controller, to add customer to the order
    {
        System.out.println("Log into your account: [enter your name]");
        String name = inputString();
        System.out.println("& [enter your password]");
        String code = inputString();
        oCtr.addCustomer(name, code);
        inputDefault();
    }

    private void newCustomer()  //method-call to order-controller, to create a new account and add it to the order
    {
        System.out.println("Enter name: [enter your name]");
        String name = inputString();
        System.out.println("Enter prefered password: [enter password]");
        String code = inputString();
        System.out.println("Enter address: [enter your address]");
        String address = inputString();
        System.out.println("Enter payment info [enter your cardID]");
        int paymentInfo = inputInt();
        System.out.println("Enter emailaddress [enter your email]");
        String email = inputString();
        System.out.println("Enter phonenumber [enter your phonenumber]");
        int tlf = inputInt();

        oCtr.createCustomer(name, code, address, paymentInfo, email, tlf);
        oCtr.addCustomer(name, code);
    }

    private void showOrder()  //method-call to order-controller, to show the order
    {
        oCtr.showOrder();
        inputDefault();
    }
    
    private void checkOut()  //method-call to order-controller, to check out the order
    {  
    oCtr.checkOut();
    }

    private String inputString()  //method which allows user to input a text
    {
        //// creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        String text = keyboard.nextLine();
        return text;
    }

    public int inputInt()  //method which allows user to input a number
    {   
        boolean ok = false;
        int number = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            try{
                number = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return number;
    }

    public void inputDefault()  //used to make a break in the code. Press enter to continue
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Press ENTER to continue: ");
        String enter = keyboard.nextLine();
    }
}
